package cn.openea.perpartner.minrest.controller;

import cn.openea.perpartner.minrest.service.RestService;
import com.alibaba.fastjson.JSONObject;

import cn.openea.perpartner.common.core.exception.ServiceException;
import cn.openea.perpartner.common.core.utils.StringUtils;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.common.log.annotation.Log;
import cn.openea.perpartner.common.log.enums.BusinessType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;


/**
 * @author Weird077
 * @description TODO
 * @date 2022-06-01 23:06
 */
@RestController
public class MinRestController extends BaseController {
    Logger logger = LoggerFactory.getLogger(MinRestController.class);
    @Autowired
    private RestService restService;


    @Log(title = "MinRest.getResult", businessType = BusinessType.INSERT)
    @PostMapping("/minrest")
    public AjaxResult minRest(String formData) throws Exception {
        JSONObject obj = JSONObject.parseObject(formData);
        String beanName = obj.getString("beanName");
        obj.put("startTime",System.currentTimeMillis());
        if (StringUtils.isEmpty(beanName)){
            throw new ServiceException("beanName不能为空!");
        }
        Future<Object> future = restService.sendWithFixedReplay(obj.getString("systemid"),obj);
        return JSONObject.toJSON(future.get()).equals(obj)?error():success(future.get());
    }

    @Log(title = "MinRest.getList", businessType = BusinessType.INSERT)
    @PostMapping("/minrest/getList")
    public TableDataInfo minRestList(String formData) throws Exception {
        JSONObject obj = JSONObject.parseObject(formData);
        String beanName = obj.getString("beanName");
        if (StringUtils.isEmpty(beanName)){
            throw new ServiceException("beanName不能为空!");
        }
        Future<Object> future = restService.sendWithFixedReplay(obj.getString("systemid"),obj);
        return JSONObject.toJSON(future.get()).equals(obj)?getDataTable(new ArrayList<>()):getDataTable((List<?>) future.get());
    }

}
